package cn.tedu.ems.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;


@Configuration
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
                //基于token,不需要csrf,所以关闭
                .csrf().disable()
                //不通过session获取SecurityContext
                .authorizeRequests(   authorize -> authorize
                        // 请求放开
                        // 把登录接口放行(允许匿名访问登录接口)
                        .antMatchers("/v1/ems/login").permitAll()
                        // 放行所有接口
                        .anyRequest().permitAll())
                        .build();
                        // 除了上述的接口可以匿名访问,其他地址的访问均需验证权限
//                        .anyRequest().authenticated()).build();
    }

    //为了认证Authentication对象
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


}

